package com.app.service;

import static org.apache.commons.lang.StringUtils.isNotEmpty;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.bean.Movies;
import com.app.bean.MoviesExample;
import com.app.bean.NewsExample;
import com.app.bean.MoviesExample.Criteria;
import com.app.bean.News;
import com.app.bean.Orders;
import com.app.bean.OrdersExample;
import com.app.bean.Ticket;
import com.app.bean.TicketExample;
import com.app.mapper.MoviesMapper;
import com.app.mapper.NewsMapper;
import com.app.mapper.OrdersMapper;
import com.app.mapper.TicketMapper;
import com.github.pagehelper.PageHelper;

@Service
public class MovieService {
	
	@Autowired
	MoviesMapper mapper; 
	
	@Autowired
	TicketMapper tkMapper;
	
	@Autowired
	OrdersMapper orderMapper;
	
	@Autowired
	NewsMapper 	newsMapper;
	

	public List<Movies> list(String moviename) {
		
		MoviesExample ex = new MoviesExample();
		Criteria cr = ex.createCriteria();
		
		if(isNotEmpty(moviename)){
			cr.andMovienameLike("%"+moviename+"%");
		}
		
		return mapper.selectByExample(ex);
	}

	public int add(Movies movie) {
		
		mapper.insert(movie);

		return 0;
		
	}

	public void delete(String id) {
		mapper.deleteByPrimaryKey(Integer.parseInt(id));
	}

	public Movies getById(String id) {
		return mapper.selectByPrimaryKey(Integer.parseInt(id));
	}

	public void update(Movies movie) {
		mapper.updateByPrimaryKey(movie);
	}
	

	public List<Ticket> getTicketsByMovieId(String id) {
		
		TicketExample ex = new TicketExample();
		com.app.bean.TicketExample.Criteria createCriteria = ex.createCriteria();
		createCriteria.andMovieidEqualTo(Integer.parseInt(id));
		
		List<Ticket> selectByExample = tkMapper.selectByExample(ex);
		
		return selectByExample;
	}

	public void createTickets(String id) {
		Movies m = mapper.selectByPrimaryKey(Integer.parseInt(id));
		
		for (int i = 0; i < 36; i++) {
			Ticket tk = new Ticket();
			tk.setMovieid(m.getId());
			tk.setMoviename(m.getMoviename());
			tk.setOccupy("空余座位");
			tk.setSeat((i+1) + "座");
			tk.setStatus("待出票");
			tkMapper.insert(tk);
			System.out.println(tk.getSeat() + "生成成功！");
		}
		
		
	}

	public void addOrder(String id) {
		//先改变那张电影票的状态
		Ticket ticket = tkMapper.selectByPrimaryKey(Integer.parseInt(id));
		ticket.setOccupy("售出座位");
		tkMapper.updateByPrimaryKey(ticket);
		
		//再生成新的订单
		Orders order = new Orders();
		order.setCreatedate(new Date());
		order.setMovieid(ticket.getMovieid());
		order.setMoviename(ticket.getMoviename());
		order.setSeat(ticket.getSeat());
		order.setStatus("0");
		
		orderMapper.insert(order);
		
	}

	public void update(String id, String column) {
		Movies movie = getById(id);
		if(column.equals("zsqd")){
			movie.setIszsqd("1");
			movie.setUpdatedate(new Date());
			mapper.updateByPrimaryKey(movie);
		}else if(column.equals("hprc")){
			movie.setIshprc("1");
			movie.setUpdatedate(new Date());
			mapper.updateByPrimaryKey(movie);
		}else if(column.equals("jrpf")){
			movie.setIsjrpf("1");
			movie.setUpdatedate(new Date());
			mapper.updateByPrimaryKey(movie);
		}
	}

	public List<Object> queryList(String type) {
		PageHelper.startPage(1, 10);
		MoviesExample ex = new MoviesExample();
		Criteria cr = ex.createCriteria();
		
		if(type.equals("jrpf")){
			cr.andIsjrpfEqualTo("1");
		}else if(type.equals("zsqd")){
			cr.andIszsqdEqualTo("1");
		}else if(type.equals("hprc")){
			cr.andIshprcEqualTo("1");
		}else if(type.equals("news")){
			PageHelper.startPage(1, 3);
			NewsExample ex2 = new NewsExample();
			ex2.setOrderByClause("publishDate desc");
			List news = newsMapper.selectByExample(ex2);
			return news;
		}
		
		ex.setOrderByClause("updatedate desc");
		
		List selectByExample = mapper.selectByExample(ex);
		
		return selectByExample;
	}

	public List<Orders> listOrder() {
		OrdersExample ex = new OrdersExample();
		ex.setOrderByClause("createdate desc");
		return orderMapper.selectByExample(ex);
	}
	
	

}
